<template>
  <div class="HyForm">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-row>
      <template v-for="item in formItems" :key="item.label">
        <el-col v-bind="colayout">
          <template v-if="item.type === 'input'">
            <el-form
              :label-position="labelPosition"
              :label-width="labelWidth"
              v-if="!item.isHidden"
            >
              <el-form-item :label="item.label">
                <el-input
                  v-model="formdata[`${item.field}`]"
                  :placeholder="item.placeholder"
                />
              </el-form-item>
            </el-form>
          </template>
          <template v-else-if="item.type === 'select'">
            <el-form>
              <el-form-item :label="item.label" :style="itemStyle">
                <el-select>
                  <el-option
                    v-for="option in item.options"
                    :key="option.value"
                    :value="option.value"
                  >
                    {{ option.title }}
                  </el-option>
                </el-select>
              </el-form-item>
            </el-form>
          </template>
          <template v-else-if="item.type === 'datepicker'">
            <el-form>
              <el-form-item :label="item.label" :style="itemStyle">
                <el-date-picker
                  style="width: 100%"
                  v-bind="item.otherOptions"
                  v-model="modelValue[`${item.field}`]"
                ></el-date-picker>
              </el-form-item>
            </el-form>
          </template>
        </el-col>
      </template>
    </el-row>
    <div class="footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script>
import { toRefs, reactive, computed, watch, ref } from "vue";
export default {
  name: "HyForm",
  props: {
    formItems: {
      type: Array,
      default: () => [],
    },
    colayout: {
      type: Object,
      default: () => {
        return {
          xl: 6, // >1920px 4个
          lg: 8,
          md: 12,
          sm: 24,
          xs: 24,
        };
      },
    },
    labelWidth: {
      type: String,
      default: "100px",
    },
    modelValue: {
      type: Object,
      required: true,
    },
    itemStyle: {
      type: Object,
      default: () => {
        return {
          padding: "10px 20px",
        };
      },
    },
    labelPosition: {
      type: String,
      default: "",
    },
  },
  setup(props, { emit }) {
    const formdata = ref({ ...props.modelValue });
    watch(
      formdata,
      (newVal) => {
        emit("update:modelValue", newVal);
      },
      { deep: true }
    );
    return {
      formdata,
    };
  },
};
</script>

<style lang="scss" scope>
.el-row {
  margin-bottom: 20px;
}
.el-row:last-child {
  margin-bottom: 0;
}
.el-col {
  border-radius: 4px;
  display: flex;
  align-items: center;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
